package com.dbh.company;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test3 {

    private static final String API_URL = "http://preview-buildinghouse.ke.com/test/mr/getOrder";

    public static void main(String[] args) throws IOException {
        exportOrderData();
    }

    /**
     * 导出订单数据到Excel
     */
    private static void exportOrderData() throws IOException {
        System.out.println("开始请求接口...");
        
        // 发送GET请求
        String response = HttpRequest.get(API_URL)
                .execute()
                .body();
        
        System.out.println("接口响应: " + response);
        
        // 解析JSON响应
        JSONObject jsonObject = JSONUtil.parseObj(response);
        Integer errNo = jsonObject.getInt("errNo");
        String errMsg = jsonObject.getStr("errMsg");
        
        System.out.println("errNo: " + errNo);
        System.out.println("errMsg: " + errMsg);
        
        if (errNo == null || errNo != 1) {
            System.out.println("请求失败: " + errMsg);
            return;
        }
        
        // 获取data数组
        JSONArray dataArray = jsonObject.getJSONArray("data");
        if (dataArray == null || dataArray.isEmpty()) {
            System.out.println("没有数据可导出");
            return;
        }
        
        System.out.println("获取到 " + dataArray.size() + " 条数据");
        
        // 创建Excel写入器
        ExcelWriter writer = ExcelUtil.getBigWriter("D:\\data\\order_export.xlsx");
        
        // 获取Workbook和Sheet
        Workbook workbook = writer.getWorkbook();
        Sheet sheet = writer.getSheet();
        
        // 创建文本格式样式
        CellStyle textStyle = workbook.createCellStyle();
        DataFormat dataFormat = workbook.createDataFormat();
        textStyle.setDataFormat(dataFormat.getFormat("@")); // @ 表示文本格式
        
        // 设置列宽
        writer.setColumnWidth(0, 25);  // resblockId
        writer.setColumnWidth(1, 25);  // photographerUcid
        
        // 设置表头
        List<String> headList = new ArrayList<>();
        headList.add("resblockId");
        headList.add("photographerUcid");
        headList.add("onlineTime");
        writer.writeSecHeadRow(headList);
        
        // 写入数据并设置文本格式
        int rowIndex = 1; // 从第2行开始（第1行是表头）
        for (Object item : dataArray) {
            JSONObject itemObj = JSONUtil.parseObj(item);
            Row row = sheet.createRow(rowIndex);
            
            // resblockId - 设置为文本格式
            Cell cell0 = row.createCell(0);
            Object resblockId = itemObj.get("resblockId");
            if (resblockId != null) {
                cell0.setCellValue(resblockId.toString());
            }
            cell0.setCellStyle(textStyle);
            
            // photographerUcid - 设置为文本格式
            Cell cell1 = row.createCell(1);
            Object photographerUcid = itemObj.get("photographerUcid");
            if (photographerUcid != null) {
                cell1.setCellValue(photographerUcid.toString());
            }
            cell1.setCellStyle(textStyle);

            // photographerUcid - 设置为文本格式
            Cell cell2 = row.createCell(2);
            Object onlineTime = itemObj.get("onlineTime");
            if (onlineTime != null) {
                cell2.setCellValue(onlineTime.toString());
            }
            cell2.setCellStyle(textStyle);
            
            rowIndex++;
        }
        
        writer.close();
        
        System.out.println("\nExcel导出完成！");
        System.out.println("文件路径: D:\\data\\order_export.xlsx");
        System.out.println("共导出 " + dataArray.size() + " 条数据");
    }
}
